package com.cy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cy.common.Constant;
import com.cy.entity.ReadData;
import com.cy.entity.WriteData;
import com.cy.io.Read;
import com.cy.io.Write;
import com.cy.utils.DataUtils;

import java.io.File;
import java.net.URL;
import java.util.*;

/**
 * @Author: chenyun
 * @Date: 2021/09/07/15:00
 * @Description:
 */

//主启动类
public class Start {

    public static void main(String[] args) {
        URL url = Start.class.getProtectionDomain().getCodeSource().getLocation();
        String filePath = null;
        try {
            filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        int index = filePath.lastIndexOf("/");
        filePath = filePath.substring(0,index);
        System.out.println("path1:"+filePath);
        //System.out.println("filepath:"+filePath);
        ArrayList<String> list = queryFilePath(filePath);
        for (int i = 0; i < list.size(); i++) {
            // 如果有副本不解析
            if (list.get(i).contains("_副本")) {
                list.remove(list.get(i));
            }
            // 如果不是.xls 和 .xlsx 不解析
            if((!list.get(i).contains(".xls")) && (!list.get(i).contains(".xlsx"))){
                list.remove(list.get(i));
            }
        }

        for (String path : list) {
            System.out.println("path2:"+path);
            List<ReadData> readDataList = Read.read(path);
            Write.write(readDataList,path);
        }


//
//        String path = "C:\\Users\\cy\\Desktop\\新建文件夹\\2021-2022学年第1学期总课表(1).xls";
//        // 读取指定路径excle表，并转换格式
//        List<ReadData> readDataList = Read.read(path);
//        // 将转换格式后的表数据写入新excle表
//        System.out.println(path);
//        Write.write(readDataList,path);

    }

    public static ArrayList<String> queryFilePath(String filePath){
        ArrayList<String> es = new ArrayList<>();
        File f = new File(filePath);
        File[] ts = f.listFiles();
        File[] fs = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if(ts[i].isFile()){
                es.add(ts[i].toString());
            }
        }
        return es;
    }


}
